#include <stdio.h>
#include <stdlib.h>
#include "oled.h"
#include "gui.h"
#include "cmsis_os2.h"
#include "Oled_Show.h"
#include "gui.h"
#include "bmp.h"
#include "led_motor_control.h"

void OLED_Display_On(void)
{
	OLED_WR_Byte(0X8D,OLED_CMD);
	OLED_WR_Byte(0X14,OLED_CMD);
	OLED_WR_Byte(0XAF,OLED_CMD);
}

void OLED_Display_Off(void)
{
	OLED_WR_Byte(0X8D,OLED_CMD);
	OLED_WR_Byte(0X10,OLED_CMD);
	OLED_WR_Byte(0XAE,OLED_CMD);
}

static void OLED_GPIO_Init(void)
{
    int ret;
   
    //连接OLED屏幕的CS引脚，负责使能OLED屏幕 
    ret = hi_io_set_func(HI_IO_NAME_GPIO_8, HI_IO_FUNC_GPIO_8_GPIO);
    if (ret != HI_ERR_SUCCESS) {
        printf("===== ERROR ===== gpio -> hi_io_set_func ret:%d\r\n", ret);
        return;
    }
    printf("----- io set func success-----\r\n");
	
    ret = hi_gpio_set_dir(HI_GPIO_IDX_8, HI_GPIO_DIR_OUT);
    if (ret != HI_ERR_SUCCESS) {
        printf("===== ERROR ===== gpio -> hi_gpio_set_dir1 ret:%d\r\n", ret);
        return;
    }
    printf("----- gpio set dir success! -----\r\n");

    //连接OLED屏幕的DC引脚，负责对OLED屏幕进行指令控制
    ret = hi_io_set_func(HI_IO_NAME_GPIO_11, HI_IO_FUNC_GPIO_11_GPIO);
    if (ret != HI_ERR_SUCCESS) {
        printf("===== ERROR ===== gpio -> hi_io_set_func ret:%d\r\n", ret);
        return;
    }
    printf("----- io set func success-----\r\n");
   
    ret = hi_gpio_set_dir(HI_GPIO_IDX_11, HI_GPIO_DIR_OUT);
    if (ret != HI_ERR_SUCCESS) {
        printf("===== ERROR ===== gpio -> hi_gpio_set_dir1 ret:%d\r\n", ret);
        return;
    }
    printf("----- gpio set dir success! -----\r\n");
}

static void Draw_Menu(float Humidity, float Temperature)
{
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    int f = 0;
    int g = 0;

    a = Temperature;
    b = a / 10;
    c = a % 10;
    d = (Temperature - a) * 10;
    
    e = Humidity;
    f = e / 10;
    g = e % 10;

    
    GUI_ShowNum(WIDTH/2-1+9,20,b,1,16,1);
    GUI_ShowNum(WIDTH/2-1+9+8,20,c,1,16,1);
	GUI_ShowString(WIDTH/2-1+9+8+8,20,".",16,1);
    GUI_ShowNum(WIDTH/2-1+9+8+16,20,d,1,16,1);
    // 湿度
    GUI_ShowNum(WIDTH/2-1+5+18,46,f,1,16,1);
    GUI_ShowNum(WIDTH/2-1+5+8+18,46,g,1,16,1);
	GUI_ShowString(WIDTH/2-1+5+8+8+18,46,"\%",16,1);

    Led_Get_Status() ? GUI_ShowString(24,20,"On ",16,1):\
                    GUI_ShowString(24,20,"Off",16,1);
    Motor_Get_Status() ? GUI_ShowString(24,46,"On ",16,1):\
                    GUI_ShowString(24,46,"Off",16,1);
    usleep(10000);
    // GUI_Fill(WIDTH/2-1+9, 20, WIDTH/2-1+9+8+16, 46, 0);
}

void UI_Init(void)
{	
	OLED_GPIO_Init();
    hi_spi_deinit(HI_SPI_ID_0);
    screen_spi_master_init(0);
    OLED_Init();			   //初始化OLED  
    OLED_Clear(0);             //清屏（全黑）
}

void UI_Update(float* Hum, float *Temp)
{
    u8 i;
	srand(123456);
	GUI_DrawLine(0, 10, WIDTH-1, 10,1);
	GUI_DrawLine(WIDTH/2-1,11,WIDTH/2-1,HEIGHT-1,1);
	GUI_DrawLine(WIDTH/2-1,10+(HEIGHT-10)/2-1,WIDTH-1,10+(HEIGHT-10)/2-1,1);
    GUI_DrawLine(1,10+(HEIGHT-10)/2-1,WIDTH-1,10+(HEIGHT-10)/2-1,1);
	GUI_ShowString(0,1,"2022-07-08",8,18);
	GUI_ShowString(80,1,"Finaday",8,1);
    GUI_ShowString(1,13,"LED",8,1);
	GUI_ShowString(WIDTH/2-1+2,13,"TEMP",8,1);
	GUI_DrawCircle(WIDTH-1-19, 25, 1,2);
	GUI_ShowString(WIDTH-1-14,20,"C",16,1);
    GUI_ShowString(1,39,"Fan",8,1);
	GUI_ShowString(WIDTH/2-1+2,39,"HUMI",8,1);
    while(1) 
    {	
        Draw_Menu(*Hum, *Temp);
        // OLED_Clear(0); 
    }
}